import { Component } from '@angular/core';
import { NavController,AlertController } from 'ionic-angular';
import   $  from 'jquery';
import { HTTP } from '@ionic-native/http';
import { IndexPage } from '../index/index';

@Component({
  selector: 'page-zhu',
  templateUrl: 'zhuce.html'
})
export class ZhucePage {
  deta ={
      "userName":"",//注册用户名
      "userPass":"",// 注册密码
      "userPassword":"",//注册正式密码
      "userMa":"", //验证码
      'userYao':''//邀请码
      
  }
    public url:string ="http://118.31.79.20:8080/";//接口地址
    // public url:string ="http://192.168.1.114:8080/";
    public zhuceurl:string = this.url + "Stock/login/register.do";//注册地址
    public yanurl:string = this.url + "Stock/code/getCode.do";
    constructor(public navCtrl: NavController,private http: HTTP,private malert:AlertController) {
        
     }

     
    // 注册验证
    login(){
        if(this.deta.userName == "" || this.deta.userName == null || this.deta.userPass == "" || this.deta.userPass == null || this.deta.userPassword == "" || this.deta.userPassword == null){
          
            this.mWinds("您好，账户和密码不能为空");
            return false;
             
        }else if(this.deta.userPass != this.deta.userPassword){
            
             this.mWinds("您好，两次密码输入不一致!!");
            return false;
            
        }else if(this.iphone() && this.passw()&& this.yma()){
            // alert("注册成功，跳转登录页面!!");
            // var headers = new Headers();
            // headers.append('Content-Type', 'application/json');
             this.http.post(this.zhuceurl,{"mobile":this.deta.userName,"password":this.deta.userPassword,"code":this.deta.userMa,'yqm':this.deta.userYao},{})   //接口。发送的数据
                .then(data => {
                let root=JSON.parse(data.data);
                // console.log("请求成功");
                // alert(root.flag); // 取到状态值 
               if(root.flag){    //成功跳转等页面
                     
                    this.mWinds("注册成功，请登录!!");
                    this.navCtrl.push(IndexPage);
               }else{
                //    alert(root.data);
                   return false;
               }
              
            }).catch(error => {
               
                 this.mWinds("网络异常");
            
            
            });
                   

        } 
        
    }
    // 验证码
    ionViewDidLoad(){
        // var autoRandom =document.querySelector(".yan");
                // autoRandom.innerHTML=result;
          this.ma();
    }
            
  // 更换验证码
  ma(){
    //   随机获取验证码
       this.http.get(this.yanurl, {}, {})
             .then(data => {
                //获取的数据
                var datass=JSON.parse(data.data);
                var result = datass.data; //验证码
                // alert("验证码"+datass.data);
               var autoRandom =document.querySelector(".yan");
               autoRandom.innerHTML=result;
            })
            .catch(error => {
                        this.mWinds("网络异常");     
               
            });     
                     
  }

  iphone(){
     // 手机号正则验证
      let regTel = /^0?1[3|4|5|8][0-9]\d{8}$/;
      if(!regTel.test(this.deta.userName)){
         
           this.mWinds("您好，账号格式不正确!!");
          return false;
      }else{
          return true;
      }
  }

  passw(){
      let iph = /^(?!(?:\d*$))[A-Za-z0-9]{6,20}$/;
      if(!iph.test(this.deta.userPass)){
         
         this.mWinds("您好，密码格式不正确!!");
          return false;
      }else{
          return true;
      }
  }
  yma(){
    let mas = $(".yan").text();
        if(this.deta.userMa != mas){
           
          this.mWinds("验证码错误!!");
            return false;
        }else if(this.deta.userMa == '' || this.deta.userMa ==null){
           
           this.mWinds("验证码不能为空!!");
            return false;
        }else{
            return true;
        }

  }
  /**
    * 自定义弹框
    */
    mWind(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,
      buttons:[{
        text:'取消',
        handler:()=>{         
        }
      },{
        text:'确定',
        handler:()=>{       
        }
      }],
      
    });
  alert.present();

    }
    /**
     * 无按钮
     * @param msg 
     */
    mWinds(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,     
    });
  alert.present();

    }

  
}
